﻿using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Dyn.News.Models;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;

namespace Dyn.News.Drivers {
    [UsedImplicitly]
    public class NewsCategoryPartDriver : ContentPartDriver<NewsCategoryPart> {
        protected override DriverResult Display(NewsCategoryPart part, string displayType, dynamic shapeHelper) {
            return Combined(
                ContentShape("Parts_Category_Category_Manage",
                    () => shapeHelper.Parts_Category_Category_Manage(ContentPart: part)),
                ContentShape("Parts_Category_Category_Description",
                    () => shapeHelper.Parts_Category_Category_Description(ContentPart: part, Description: part.Description)),
                ContentShape("Parts_Category_Category_SummaryAdmin",
                    () => shapeHelper.Parts_Category_Category_SummaryAdmin(ContentPart: part, ContentItem: part.ContentItem)),
                ContentShape("Parts_Category_Category_ArticleCount",
                    () => shapeHelper.Parts_Category_Category_ArticleCount(ContentPart: part, PostCount: part.PostCount))
                );
        }

        protected override DriverResult Editor(NewsCategoryPart CategoryPart, dynamic shapeHelper) {
            var results = new List<DriverResult> {
                ContentShape("Parts_Category_Category_Fields",
                             () => shapeHelper.EditorTemplate(TemplateName: "Parts.Category.Category.Fields", Model: CategoryPart, Prefix: Prefix)),
            };

            
            if (CategoryPart.Id > 0)
                results.Add(ContentShape("Category_DeleteButton",
                    deleteButton => deleteButton));

            return Combined(results.ToArray());
        }

        protected override DriverResult Editor(NewsCategoryPart CategoryPart, IUpdateModel updater, dynamic shapeHelper) {
            updater.TryUpdateModel(CategoryPart, Prefix, null, null);
            return Editor(CategoryPart, shapeHelper);
        }

        protected override void Importing(NewsCategoryPart part, Orchard.ContentManagement.Handlers.ImportContentContext context) {
            var description = context.Attribute(part.PartDefinition.Name, "Description");
            if (description != null) {
                part.Description = description;
            }

            var postCount = context.Attribute(part.PartDefinition.Name, "PostCount");
            if (postCount != null) {
                part.PostCount = Convert.ToInt32(postCount);
            }
            var parent= context.Attribute(part.PartDefinition.Name, "ParentID");
            if (postCount != null)
            {
                part.PostCount = Convert.ToInt32(postCount);
            }
            else
                part.ParentID = 0;
        }

        protected override void Exporting(NewsCategoryPart part, Orchard.ContentManagement.Handlers.ExportContentContext context) {
            context.Element(part.PartDefinition.Name).SetAttributeValue("Description", part.Description);
            context.Element(part.PartDefinition.Name).SetAttributeValue("PostCount", part.PostCount);
        }
    }
}